package com.battery.system.service.battery.impl;

import com.battery.common.core.text.Convert;
import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.MemberBO;
import com.battery.system.domain.Member;
import com.battery.system.domain.Merchant;
import com.battery.system.domain.Store;
import com.battery.system.domain.VIPDetails;
import com.battery.system.domain.VO.MemberVO;
import com.battery.system.mapper.MemberMapper;
import com.battery.system.service.battery.IMemberService;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * VIP申请Service业务层处理
 *
 * @author battery
 * @date 2021-01-22
 */
@Service
public class MemberServiceImpl extends BaseServiceImpl<Member> implements IMemberService {
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询VIP申请
     *
     * @param id VIP申请ID
     * @return VIP申请
     */
    @Override
    public Member selectMemberById(String id) {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询VIP申请列表
     *
     * @param member VIP申请
     * @return VIP申请
     */
    @Override
    public List<Member> selectMemberList(Member member) {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增VIP申请
     *
     * @param member VIP申请
     * @return 结果
     */
    @Override
    public int insertMember(Member member) {
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改VIP申请
     *
     * @param member VIP申请
     * @return 结果
     */
    @Override
    public int updateMember(Member member) {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 删除VIP申请对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(String ids) {
        return memberMapper.deleteMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除VIP申请信息
     *
     * @param id VIP申请ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(String id) {
        return memberMapper.deleteMemberById(id);
    }

    /**
     * 查看VIP客户列表数据
     *
     * @param memberBO
     * @return
     */
    @Override
    public List<MemberVO> selectMemberVOList(MemberBO memberBO) {
        return memberMapper.selectMemberVOList(memberBO);
    }

    @Override
    public List<VIPDetails> selectMemberByKeyList(String id, String key) {
        return memberMapper.selectMemberByKeyList(id, key);
    }

    @Override
    public VIPDetails selectVIPDetailsById(String id) {
        return memberMapper.selectVIPDetailsById(id);
    }

    @Override
    public List<Member> selectMemberOnStoreNameList(Member member) {
        return memberMapper.selectMemberOnStoreNameList(member);
    }

    /**
     * 校验该用户是否已经申请过VIP
     *
     * @param appUserId
     * @return
     */
    @Override
    public Member checkMember(String appUserId) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("appUserId", appUserId);
        criteria.andEqualTo("isDelete", 0); // 未删除
        // 审批状态 0 未审批 1 已通过 2 已拒绝
        List<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(0);
        criteria.andIn("auditStatus", integers);
        return memberMapper.selectOneByExample(example);
    }

    /**
     * 查询用户VIP信息
     *
     * @param appUserId
     * @return
     */
    @Override
    public Member selectTrueMember(String appUserId) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("appUserId", appUserId);
        criteria.andEqualTo("isDelete", 0); // 未删除
        // 审批状态 0 未审批 1 已通过 2 已拒绝
        criteria.andEqualTo("auditStatus", 1);
        return memberMapper.selectOneByExample(example);
    }

    /**
     * 查询该合作商是否存在未审批的vip客户申请
     *
     * @param partnerId
     * @return
     */
    @Override
    public int selectVipInOnCountByPartnerId(String partnerId) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("partnerId", partnerId);
        criteria.andEqualTo("isDelete", 0); // 未删除
        // 审批状态 0 未审批 1 已通过 2 已拒绝
        criteria.andEqualTo("auditStatus", 0);
        return (memberMapper.selectCountByExample(example));
    }


}
